package com.ruoyi.tuikuanguanli.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.tuikuanguanli.domain.RefundRecords;
import com.ruoyi.tuikuanguanli.service.IRefundRecordsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 退款管理Controller
 * 
 * @author ruoyi
 * @date 2025-09-09
 */
@RestController
@RequestMapping("/tuikuanguanli")
public class RefundRecordsController extends BaseController
{
    @Autowired
    private IRefundRecordsService refundRecordsService;

    /**
     * 查询退款管理列表
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:list")
    @GetMapping("/list")
    public TableDataInfo list(RefundRecords refundRecords)
    {
        startPage();
        List<RefundRecords> list = refundRecordsService.selectRefundRecordsList(refundRecords);
        return getDataTable(list);
    }

    /**
     * 导出退款管理列表
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:export")
    @Log(title = "退款管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RefundRecords refundRecords)
    {
        List<RefundRecords> list = refundRecordsService.selectRefundRecordsList(refundRecords);
        ExcelUtil<RefundRecords> util = new ExcelUtil<RefundRecords>(RefundRecords.class);
        util.exportExcel(response, list, "退款管理数据");
    }

    /**
     * 获取退款管理详细信息
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(refundRecordsService.selectRefundRecordsById(id));
    }

    /**
     * 新增退款管理
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:add")
    @Log(title = "退款管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RefundRecords refundRecords)
    {
        return toAjax(refundRecordsService.insertRefundRecords(refundRecords));
    }

    /**
     * 修改退款管理
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:edit")
    @Log(title = "退款管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RefundRecords refundRecords)
    {
        return toAjax(refundRecordsService.updateRefundRecords(refundRecords));
    }

    /**
     * 删除退款管理
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:remove")
    @Log(title = "退款管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(refundRecordsService.deleteRefundRecordsByIds(ids));
    }

    /**
     * 同意退款
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:edit")
    @Log(title = "同意退款", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{id}")
    public AjaxResult approveRefund(@PathVariable("id") Long id)
    {
        RefundRecords refundRecord = refundRecordsService.selectRefundRecordsById(id);
        if (refundRecord == null) {
            return error("退款记录不存在");
        }
        if (!"pending".equals(refundRecord.getRefundStatus())) {
            return error("该退款申请状态不允许操作");
        }
        
        refundRecord.setRefundStatus("refunded");
        int result = refundRecordsService.updateRefundRecords(refundRecord);
        
        if (result > 0) {
            // 这里可以添加其他业务逻辑，比如：
            // 1. 更新订单状态
            // 2. 调用支付接口退款
            // 3. 发送通知等
            return success("退款申请已同意");
        } else {
            return error("操作失败");
        }
    }

    /**
     * 拒绝退款
     */
    @RequiresPermissions("tuikuanguanli:tuikuanguanli:edit")
    @Log(title = "拒绝退款", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{id}")
    public AjaxResult rejectRefund(@PathVariable("id") Long id, @RequestBody(required = false) java.util.Map<String, String> params)
    {
        RefundRecords refundRecord = refundRecordsService.selectRefundRecordsById(id);
        if (refundRecord == null) {
            return error("退款记录不存在");
        }
        if (!"pending".equals(refundRecord.getRefundStatus())) {
            return error("该退款申请状态不允许操作");
        }
        
        String reason = params != null ? params.get("reason") : "不符合退款条件";
        refundRecord.setRefundStatus("rejected");
        refundRecord.setRefundReason(refundRecord.getRefundReason() + " (拒绝原因: " + reason + ")");
        int result = refundRecordsService.updateRefundRecords(refundRecord);
        
        if (result > 0) {
            return success("退款申请已拒绝");
        } else {
            return error("操作失败");
        }
    }
}
